#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>

#define MYPORT  43210
#define BUFFER_SIZE 1024

int sock_cli;
int res;
pthread_t a_thread;
void* thread_result;

void handofpipesig(int signal)
{
	printf("[%d]\n", signal);
	printf("%s\n", "Out of the process");
	exit(0);
	return;
}
void* thread_function(void *arg)
{
    sleep(20);
    printf("I am here to close the socketConn\n");
    close(sock_cli);
    pthread_exit(0);
}
void newThread()
{
    res = pthread_create(&a_thread, NULL, thread_function, NULL);
    if(res != 0)
    {
        perror("Thread creation failed");
        exit(0);
    }
    return;
}
int main()
{

    sock_cli = socket(AF_INET,SOCK_STREAM, 0);

    struct sockaddr_in servaddr;
    char a = 'a';
    int flag;

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(MYPORT);
    servaddr.sin_addr.s_addr = inet_addr("10.210.106.46");
    if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("connect");
        exit(1);
    }
    signal(SIGPIPE, handofpipesig);
    //newThread();
    while (1)
    {
    	printf("I want send [%c]\n", a);
        flag = send(sock_cli, &a, 1, 0);
        printf("send:[%d]||data:[%d]\n", flag, a);
        a++;
        break;
    }

    close(sock_cli);
    return 0;
}